from itsdangerous import BadData, TimedJSONWebSignatureSerializer
from django.conf import settings


# 加密
def check_open_id(openid):
    serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=600)
    access_token = serializer.dumps({'openid': openid}).decode()
    return access_token


# 解密
def check_access_token(access_token):
    serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=600)
    try:
        data = serializer.loads(access_token)
    except BadData:
        # 验证失败
        return None
    else:
        return data.get('openid')
